
package org.columba.mail.filter.plugins;

import org.columba.addressbook.facade.IContactFacade;
import org.columba.addressbook.facade.IContactItem;
import org.columba.api.exception.ServiceNotFoundException;
import org.columba.core.filter.AbstractFilter;
import org.columba.core.filter.FilterCriteria;
import org.columba.core.filter.IFilterCriteria;
import org.columba.core.folder.api.IFolder;
import org.columba.mail.connector.ServiceConnector;
import org.columba.mail.folder.IMailbox;
import org.columba.ristretto.message.Address;
import org.columba.ristretto.message.Header;
import org.columba.ristretto.parser.AddressParser;
import com.deruan.hotcore.contact.model.ContactModelFactory;

/**
 * Check if sender is in all available addressbooks.
 * 
 * @author fdietz
 * 
 */
public class AddressbookFilter extends AbstractFilter {

	public AddressbookFilter() {

	}

	/**
	 * @see org.columba.core.filter.AbstractFilter#process(IFolder,
	 *      java.lang.Object)
	 */
	public boolean process(IFolder folder, Object uid) throws Exception {
		Header header = ((IMailbox) folder).getHeaderFields(uid,
				new String[] { "From" });
		String from = header.get("From");

		Address address = null;
		try {
			address = AddressParser.parseAddress(from);
		} catch (Exception ex) {
			return false;
		}
		String contactId = ContactModelFactory.getInstance().findByEmailAddress(address.getMailAddress());
		if ( contactId != null ) return true;
		
		return false;
	}

	/**
	 * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
	 */
	public void setUp(IFilterCriteria f) {

	}
}